home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Views / Includes / UMenuView.h < prev    next >
Encoding:
Text File  |  1996-04-03  |  8.0 KB  |  222 lines  |  [TEXT/MPS ]

  1. // UMenuView.h
  2. // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
  3.  
  4. #ifndef __UMENUVIEW__
  5. #define __UMENUVIEW__
  6.  
  7. //----------------------------------------------------------------------------------------
  8. // Theory of Operation
  9. //
  10. // This is Larry Rosenstein's excellent unit: UMenu, that supports an object-oriented way
  11. // of defining custom menus. Instead of writing a menu defproc, you simply create a
  12. // subtype of TMenuView and override certain methods. Not only is this easier to program,
  13. // but this unit takes care of some of the housekeeping that a menu defproc would normally
  14. // have to do.
  15. //
  16. // Changes since MacApp 2.0: You can now add subviews to your subclass of TMenuView, and
  17. // these subviews can have their own trackers. This is accomplished in HandleChooseMessage
  18. // by creating a dummy mouse down event and passing it to the TMenuView via the
  19. // HandleMouseDown method which then asks its subviews if any of them are interested in
  20. // the mousedown (eg. to create a tracker).
  21. //----------------------------------------------------------------------------------------
  22.  
  23. // MacApp
  24.  
  25. #ifndef __TOOLBOX__
  26. #include "Toolbox.h"
  27. #endif
  28.  
  29. #ifndef __UVIEW__
  30. #include "UView.h"
  31. #endif
  32.  
  33. // Toolbox
  34.  
  35.  
  36. //----------------------------------------------------------------------------------------
  37. // Forward and external class declarations. 
  38. //----------------------------------------------------------------------------------------
  39.  
  40.  
  41. //----------------------------------------------------------------------------------------
  42. // Constants
  43. //----------------------------------------------------------------------------------------
  44.  
  45. const short kNoMenuItem = 0;                    // used to indicate that no item is
  46.                                                 // selected
  47.  
  48.  
  49. //----------------------------------------------------------------------------------------
  50. // MenuColors: a composite of the MCEntries
  51. //----------------------------------------------------------------------------------------
  52.  
  53. struct MenuColors
  54. {
  55.     CRGBColor itemColor;                        // the foreground color to draw the item
  56.                                                 // with (the color of the title if title
  57.                                                 // or menubar)
  58.  
  59.     CRGBColor backgroundColor;                    // the background color to draw the item
  60.                                                 // with (the color of the menubar if title
  61.                                                 // or menubar)
  62.  
  63.     CRGBColor markColor;                        // the foreground color to draw the mark
  64.                                                 // with (checkmarks, etc.)
  65.  
  66.     CRGBColor commandColor;                        // the foreground color to draw the
  67.                                                 // command key equivalent with (command-v
  68.                                                 // for paste, etc).
  69. };
  70.  
  71. typedef MenuColors *MenuColorsPtr, **MenuColorsHandle;
  72.  
  73.  
  74. //----------------------------------------------------------------------------------------
  75. // TMenuView
  76. //----------------------------------------------------------------------------------------
  77.  
  78. class TMenuView : public TView
  79. {
  80.     MA_DECLARE_CLASS;
  81.     
  82. protected:
  83.     long fNextFlash;                            // Used internally
  84.  
  85. public:
  86.     MenuRef fMenuRef;                            // handle to menu itself
  87.  
  88.     long fFlashInterval;                        // Time (in ticks) between flashes of item
  89.                                                 // highlighting; default is -1 which means
  90.                                                 // only change highlighting w when item
  91.                                                 // changes
  92.  
  93.     Boolean fHighlighted;                        // True if an item is highlighted
  94.  
  95.     CRect fBorder;                                // Added to menuRect to get actual hit
  96.                                                 // CRect; FindItem will not be called if
  97.                                                 // the mouse is in the border.
  98.  
  99.  
  100.  
  101.     //------------------------------------------------------------------------------------
  102.     // Initialization
  103.     //------------------------------------------------------------------------------------
  104.  
  105.     TMenuView();
  106.         // Constructor
  107.     virtual ~TMenuView();
  108.         // Destructor
  109.         
  110.     void IMenuView(ResNumber rsrcID, short menuWidth, short menuHeight);
  111.         // IMenu reads the menu and sets up the MenuRef and defproc so that the Menu
  112.         // Manager can communicate with the TMenuView object.
  113.  
  114.  
  115.     //------------------------------------------------------------------------------------
  116.     // Menu Definition; all these methods must be overridden
  117.     //------------------------------------------------------------------------------------
  118.  
  119.     virtual short FindItem(CPoint hitPt);
  120.         // This is called to convert a CPoint to an item number.
  121.  
  122.     virtual void Highlight(short whichItem, Boolean turnItOn);
  123.         // Given an item number as returned by FindItem, this is called to highlight the
  124.         // item. This will not be called with whichItem = kNoMenuItem.
  125.  
  126.  
  127.     //------------------------------------------------------------------------------------
  128.     // Other methods
  129.     //------------------------------------------------------------------------------------
  130.  
  131.     virtual void GetMenuViewColors(ResNumber theMenu,
  132.                                           short theItem,
  133.                                           MenuColors& theMenuColors); 
  134.         // Called to get the appropriate menu colors for the menu and item. If the menu
  135.         // and item are 0 then info is for the menubar. If item is 0 then info is for the
  136.         // menu title. Attempts to return best guess at colors based on all available
  137.         // info.
  138.  
  139.     virtual Boolean IsItemEnabled(short item);
  140.         // Returns the enabled status of an item
  141.  
  142.     virtual void UpdateHighlight(short oldItem, short newItem);
  143.         // Called to update menu item highlighting.
  144.  
  145.  
  146.     //------------------------------------------------------------------------------------
  147.     // Private
  148.     //------------------------------------------------------------------------------------
  149.  
  150.     virtual void HandleDefproc(short message,
  151.                                       MenuRef theMenu,
  152.                                       CRect& menuRect,
  153.                                       CPoint hitPt,
  154.                                       short& whichItem);
  155.         // This is called by the custom defproc; it does a CASE on the message and calls 1
  156.         // or more of the methods above.
  157.  
  158.     virtual void HandleChooseMessage(short message,
  159.                                             MenuRef theMenu,
  160.                                             CRect& menuRect,
  161.                                             CPoint hitPt,
  162.                                             short& whichItem);
  163.         // Called by the message dispatcher when a choose message has been sent
  164.  
  165.     virtual void HandleDrawMessage(short message,
  166.                                           MenuRef theMenu,
  167.                                           CRect& menuRect,
  168.                                           CPoint hitPt,
  169.                                           short& whichItem);
  170.         // Called by the message dispatcher when a draw message has been sent
  171.  
  172.     virtual void HandleSizeMessage(short message,
  173.                                           MenuRef theMenu,
  174.                                           CRect& menuRect,
  175.                                           CPoint hitPt,
  176.                                           short& whichItem);
  177.  
  178.     virtual void HandlePopUpMessage(short message,
  179.                                            MenuRef theMenu,
  180.                                            CRect& menuRect,
  181.                                            CPoint hitPt,
  182.                                            short& whichItem);
  183.  
  184.  
  185.     //------------------------------------------------------------------------------------
  186.     // Focusing Methods
  187.     //------------------------------------------------------------------------------------
  188.  
  189.     virtual Boolean Focus();
  190.  
  191.     virtual Boolean FocusOnSuperView();
  192.     
  193.     virtual Boolean IsActive();    // override
  194.  
  195.  
  196.     //------------------------------------------------------------------------------------
  197.     // Misc. Methods
  198.     //------------------------------------------------------------------------------------
  199.  
  200.     virtual GrafPtr GetGrafPort();
  201.  
  202.     virtual Boolean IsShown();    // override
  203. };
  204.  
  205.  
  206. //----------------------------------------------------------------------------------------
  207. // Global functions declarations
  208. //----------------------------------------------------------------------------------------
  209.  
  210. extern void InitUMenuView();
  211.     // Call this at the start of your program, before creating any TMenuView objects. This
  212.     // can signal Failure.
  213.  
  214.  
  215. //----------------------------------------------------------------------------------------
  216. // Global variable declarations
  217. //----------------------------------------------------------------------------------------
  218.  
  219. extern Boolean gTrackingInMenu;
  220.  
  221. #endif
  222.